<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <style>
        body {
            background-image: url('/picture/register.jpg');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
            background-attachment: fixed;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 300px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: rgba(255, 255, 255, 0.9);
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        .btn {
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>用户注册</h2>
        <div class="form-group">
            <input type="text" id="username" placeholder="用户名">
        </div>
        <div class="form-group">
            <input type="password" id="password" placeholder="密码">
        </div>
        <div class="form-group">
            <input type="password" id="confirmPassword" placeholder="确认密码">
        </div>
        <div class="form-group">
            <input type="text" id="major" placeholder="专业">
        </div>
        <div class="form-group">
            <input type="text" id="className" placeholder="班级">
        </div>
        <button class="btn" onclick="register()">注册</button>
        <p>已有账号？<a href="login.html">立即登录</a></p>
    </div>
    
    <script>
        function register() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const major = document.getElementById('major').value;
            const className = document.getElementById('className').value;
            
            if (!username || !password || !major || !className) {
                alert('所有字段都必须填写！');
                return;
            }
            
            if (password !== confirmPassword) {
                alert('两次输入的密码不一致！');
                return;
            }
            
            console.log('注册信息：', {
                username,
                password,
                major,
                className
            });
            
            fetch('/api/auth/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password,
                    major: major,
                    className: className,
                    isAdmin: 0
                })
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    alert('注册成功！');
                    window.location.href = '/login.html';
                } else {
                    alert('注册失败：' + (data.message || '未知错误'));
                }
            })
            .catch(error => {
                console.error('请求错误：', error);
                alert('注册失败：' + error.message);
            });
        }
    </script>
</body>
</html> 